#!/usr/bin/python3
"""
Implant an MD5 checksum in an ISO9660 image

This stage is using implantisomd5(1) to implant MD5 checksums into an iso
image. This is needed for the check media feature used in the installer.
"""

import os
import subprocess
import sys

import osbuild.api


SCHEMA = """
"additionalProperties": false,
"required": ["filename"],
"properties": {
  "filename": {
    "type": "string",
    "description": "Path to where the iso to implant md5s is located."
  }
}
"""


def main(tree, options):
    filename = options["filename"].lstrip("/")

    cmd = [
        "implantisomd5", os.path.join(tree, filename)
    ]

    print(cmd)
    subprocess.run(cmd,
                   cwd=tree,
                   check=True)

    return 0


if __name__ == '__main__':
    args = osbuild.api.arguments()
    r = main(args["tree"], args["options"])
    sys.exit(r)
